<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 集采订单
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class JcOrderController extends CommonController{

    public function index(){
        $status = I('status');
        $order_sn = I('order_sn');
        $map = [];
        $map['order_type'] = 'goods';
        $map['del_status'] = 0;
        
        if($order_sn){
            $map['order_sn'] = ['like', '%'.$order_sn.'%'];
        }
        switch ($status) {
            case '1':
                // 待付款订单
                $map['order_status'] = 1;
                $map['pay_status'] = 0;
                break;
            case '2':
                // 待发货订单
                $map['order_status'] = 1;
                $map['pay_status'] = 1;
                $map['shipping_status'] = 0;
                break;
            case '3':
                // 待收货订单
                $map['order_status'] = 1;
                $map['shipping_status'] = 1;
                break;
            case '4':
                // 已完成订单
                $map['order_status'] = 2;
                break;
            case '5':
                // 已关闭订单
                $map['order_status'] = ['in', [-1, 0]];
                break;
            default:
                break;
        }
        list($list, $page) = $this->lists('JcOrder', $map, 'id desc');
        if($list){
            if($order_ids = array_column($list, 'id')){
                $feedback = D('Api/JcFeedback')->where(['order_id'=>['in', $order_ids]])->getField('order_id,content');
            }else{
                $feedback = [];
            }
            foreach ($list as $key => &$row) {
                $row['orser_status'] = order_status($row);
                $row['order_btn'] = order_btn($row);
                if(isset($feedback[$row['id']])){
                    $row['is_feedback'] = 1;
                    $row['feedback_content'] = $feedback[$row['id']];
                }else{
                    $row['is_feedback'] = 0;
                }
            }
        }
        $this->assign('page',$page);
        $this->assign('list', $list);
        $this->assign('order_sn', $order_sn);
        $this->display();
    }

    public function order_goods() {
        $order_sn = I('order_sn');
        $order = M('JcOrder')->where(['order_sn'=>$order_sn])->field('id,order_sn,pay_money,user_name,phone,pro,city,area,address,shipping_code,shipping_name')->find();
        $order_goods = [];
        if($order){
            $order_goods = M('JcOrderGoods')->where(['order_id'=>$order['id']])->select();
        }
        $this->ajaxReturn(['status'=>true, 'order'=>$order, 'order_goods'=>$order_goods]);
    }

    public function qr_order()
    {
        $order_id = I('order_id');
        $res = M('JcOrder')->where(['id'=>$order_id])->save(['order_status'=>'2']);
        if($res===false){
            $this->ajaxReturn(['status'=>false, 'msg'=>'提交失败']);
        }
        $this->ajaxReturn(['status'=>true, 'msg'=>'提交成功']);
    }

    public function delivery(){
        $order_id = I('order_id');
        $shipping_code = I('shipping_code');
        $shipping_name = I('shipping_name');
        if(empty($shipping_code)){
            $this->ajaxReturn(['status'=>false, 'msg'=>'请输入单号']);
        }
        if(empty($shipping_name)){
            $this->ajaxReturn(['status'=>false, 'msg'=>'请输入类型']);
        }

        $order = M('JcOrder')->where(['id'=>$order_id])->find();
        if(empty($order)){
            $this->ajaxReturn(['status'=>false, 'msg'=>'订单不存在或已被删除']);
        }
        if($order['order_status']==-1){
            $this->ajaxReturn(['status'=>false, 'msg'=>'订单交易已关闭']);
        }
        if($order['order_status']==0){
            $this->ajaxReturn(['status'=>false, 'msg'=>'订单交易已取消']);
        }
        if($order['order_status']==2){
            $this->ajaxReturn(['status'=>false, 'msg'=>'订单交易已完成']);
        }
        if($order['order_status']==1){
            if($order['shipping_status'] == 1){
                $this->ajaxReturn(['status'=>false, 'msg'=>'订单已发货']);
            }
            $order_data = [
                'shipping_code'     => $shipping_code,
                'shipping_name'     => $shipping_name,
                'shipping_status'   => 1,
                'update_time'       => time(),
            ];
            $res = M('JcOrder')->where(['id'=>$order_id])->save($order_data);
            if($res !== false){
                $this->ajaxReturn(['status'=>true, 'msg'=>'成功']);
            }else{
                $this->ajaxReturn(['status'=>false, 'msg'=>'修改失败']);
            }
        }
        $this->ajaxReturn(['status'=>false, 'msg'=>'订单状态无法修改']);
    }

    public function detail(){
        
    }
}